package com.sbj.controller;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sbj.dto.req.*;
import com.sbj.dto.res.CarouselRes;
import com.sbj.dto.res.CategoryRes;
import com.sbj.dto.res.GoodsRes;
import com.sbj.dto.res.MemberRes;
import com.sbj.entity.Goods;
import com.sbj.service.CarouselService;
import com.sbj.service.CategoryService;
import com.sbj.service.GoodsService;
import com.sbj.service.IMemberService;
import com.zmh.framework.properties.SmsProperties;


import com.zmh.framework.sms.AliSms;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/10 16:53
 */
@RestController
@RequestMapping(value = "/web")
@AllArgsConstructor
public class WebController {

   private final IMemberService memberService;
    /**
     * 会员登录
     * @return
     */
    @PostMapping(value = "/login")
    public Result<MemberRes> login(@Valid @RequestBody WebLoginReq req) {
        MemberRes memberRes = memberService.login(req);
        StpUtil.login(memberRes.getId(),"H5");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        if(StrUtil.isNotEmpty(SaManager.getConfig().getTokenPrefix())){
            tokenInfo.setTokenValue(SaManager.getConfig().getTokenPrefix()+" "+tokenInfo.getTokenValue());
        }
        memberRes.setToken(tokenInfo.getTokenValue());
        return Result.successResult(memberRes);
    }

    private final AliSms aliSms;

    private final SmsProperties  smsProperties;


    /**
     * 注册验证码
     * @param req
     * @return
     */
    @PostMapping(value = "/sendCode")
    public Result<String> regSendCode(@Valid @RequestBody SendCodeReq req) {
        aliSms.sendSms(req.getPhone());
        return Result.successResult();
    }

    /**
     * 修改密码的验证码
     * @param req
     * @return
     */
    @PostMapping(value = "/editPhonePassWord")
    public Result<String> editPhonePassWord(@Valid @RequestBody SendCodeReq req){
          smsProperties.setTemplateCode("SMS_472480404");
          aliSms.setSmsProperties(smsProperties);
          aliSms.sendSms(req.getPhone());
          return Result.successResult();
    }

    /**
     * 注册会员
     * @param req
     * @return
     */
    @PostMapping(value = "/register")
    public Result<String> register(@RequestBody @Valid RegisterReq req) {
        memberService.register(req);
        return Result.successResult();

    }

    /**
     * 忘记密码
     * @param req
     * @return
     */
    @PostMapping(value = "/forgetPsd")
    public Result<String> forgetPsd(@RequestBody  @Valid ForgetPsdReq req) {
        memberService.forgetPsd(req);
        return Result.successResult();
    }




    private final CarouselService carouselService;

    /**
     * 轮播图
     * @return
     */
    @GetMapping(value = "/carouseLists")
    public Result<List<CarouselRes>>  carouseLists(){
        return Result.successResult(Convert.toList(CarouselRes.class,carouselService.list()));
    }

    private final CategoryService categoryService;

    /**
     * 类型列表
     * @return
     */
    @GetMapping(value = "/categoryList")
    public Result<List<CategoryRes>>  categoryList(){
        return Result.successResult(Convert.toList(CategoryRes.class,categoryService.list()));
    }

    private final GoodsService goodsService;

    /**
     * 商品列表
     * @param goodsReq
     * @return
     */
    @PostMapping(value = "/goodsList")
    public Result<Page<GoodsRes>> goodsList(@RequestBody GoodsReq goodsReq){
        return Result.successResult(goodsService.lists(goodsReq));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping(value = "/findGoodsById/{id}")
    public Result<GoodsRes> findGoodsById(@PathVariable Integer id){
        return Result.successResult(Convert.convert(GoodsRes.class,goodsService.getById(id)));
    }


   @PostMapping(value = "/sign")
   public Result<String> sign(@RequestBody SignReq signReq){
       String sign = memberService.sign(signReq);
       return Result.successResult(sign);
   }

}
